<?php
declare (strict_types = 1);

namespace app\api\model\v1;

use think\Db;
use think\Model;

/**
 * @mixin \think\Model
 * 社区
 */
class Community extends AppModel
{
    //关联关系

    //所属区域
    public function area(){
        return $this->belongsTo(Area::class,'area_id');
    }

    //所属街道
    public function street(){
        return $this->belongsTo(Street::class,'street_id');
    }

    //包保单位or采集单位
    public function gatherCompany(){

        return $this->hasOne(GatherCompany::class,'id');
    }

    //关联人员
    public function admin(){
        return $this->hasMany(Admins::class,'unit_id');
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/29 14:14
     * @param $arrData
     * @param $create_id
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 县区，街道，社区，单位 入库
     */
    public static function saveImportExcel($arrData, $create_id)
    {
        if (!empty($arrData)) {
            foreach ($arrData as $key => $value) {
                //所属县区
                $res = Area::where(['area_title' => $value['a']])->find();
                if (is_null($res)) {
                    $area_id = Area::create([
                        'area_title'=>$value['a'],
                        'uid'=>$create_id
                    ])->id;
                }else{
                    $area_id = $res->id;
                }
                if (isset($value['item'])) { //判断是否包含item字段 为街道
                    //循环街道添加入库 街道
                    foreach ($value['item'] as $child_key => $child_value) {
                        $street_res = Street::where(['street_title'=>$child_value['b']])->find();
                        if (is_null($street_res)) {
                            $street_id = Street::create([
                                'street_title'=>$child_value['b'],
                                'area_id'=>$area_id,
                                'uid'=>$create_id
                            ])->id;
                        }else{
                            $street_id = $street_res->id;
                        }
                        if (isset($child_value['item'])) { //判断是否包含item字段 为社区
                            foreach ($child_value['item'] as $child_child_key => $child_child_value) {
                                //社区
                                $community_res = self::where(['community_title'=>$child_child_value['c']])->find();
                                if (is_null($community_res)) {
                                    $community_id = self::create([
                                        'community_title'=>$child_child_value['c'],
                                        'area_id'=>$area_id,
                                        'street_id'=>$street_id,
                                        'uid'=>$create_id
                                    ])->id;
                                }else{
                                    $community_id = $community_res->id;
                                }
                                //包保单位or采集单位
                                $company_res = GatherCompany::where(['gather_company_title'=>$child_child_value['d']])->find();
                                if (is_null($company_res)) {
                                    $gather_company_id = GatherCompany::create([
                                        'gather_company_title'=>$child_child_value['d'],
                                        'area_id'=>$area_id,
                                        'street_id'=>$street_id,
                                        'community_id'=>$community_id,
                                        'uid'=>$create_id
                                    ])->id;
                                }else{
                                    $gather_company_id = $company_res->id;
                                }
//                                //更新当前社区表的包保单位id入表
//                                $community = Community::find($community_id);
//                                $community->save([
//                                    'id'=>$community_id,
//                                    'gather_company_id'=>$gather_company_id
//                                ]);
                            }
                        }
                    }
                }
            }
            $result = 200;
        } else {
            $result = 500;
        }
        return $result;
    }
}
